/* libfake437 - a library for the rendering of code page 437 `graphics'
 * Copyright (C) 2008 Jack Kelly <endgame.dos@gmail.com>
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#ifdef HAVE_STRING_H
#  include <string.h>
#endif
#include "fake437.h"

Uint8 f437_GetChar(FAKE437 *fake437, Uint16 x, Uint16 y){
  return fake437->cells[F437_INDEX(fake437, x, y)].ch;
}

Uint8 f437_GetForeground(FAKE437 *fake437, Uint16 x, Uint16 y){
  return fake437->cells[F437_INDEX(fake437, x, y)].fg;
}

Uint8 f437_GetBackground(FAKE437 *fake437, Uint16 x, Uint16 y){
  return fake437->cells[F437_INDEX(fake437, x, y)].bg;
}

char *f437_GetCharDef(FAKE437 *fake437, Uint8 ch){
  return strdup(fake437->chars[ch]);
}

f437_charDefs *f437_GetCharDefs(FAKE437 *fake437){
  f437_charDefs *chars = malloc(sizeof(f437_charDefs));
  if(chars != NULL) memcpy(chars, &(fake437->chars), sizeof(f437_charDefs));
  return chars;
}

SDL_Color *f437_GetColorDef(FAKE437 *fake437, Uint8 col){
  SDL_Color *out = malloc(sizeof(SDL_Color));
  if(out != NULL) memcpy(out, &(fake437->colors[col]), sizeof(SDL_Color));
  return out;
}

f437_colorDefs *f437_GetColorDefs(FAKE437 *fake437){
  f437_colorDefs *colors = malloc(sizeof(f437_colorDefs));
  if(colors != NULL) memcpy(colors, &(fake437->colors), sizeof(f437_colorDefs));
  return colors;
}

void f437_GetOffsets(FAKE437 *fake437, Sint16 *offsetx, Sint16 *offsety){
  *offsetx = ((fake437)->surface->w - F437_CHAR_WIDTH * fake437->width) / 2;
  *offsety = ((fake437)->surface->h - F437_CHAR_HEIGHT * fake437->height) / 2;
}

void f437_GetCellCoords(FAKE437 *fake437, Uint16 x, Uint16 y,
			Sint16 *out_x, Sint16 *out_y){
  Sint16 offsetx;
  Sint16 offsety;
  f437_GetOffsets(fake437, &offsetx, &offsety);
  if((x < offsetx) || (x > F437_CHAR_WIDTH * fake437->width + offsetx))
    *out_x = -1;
  else
    *out_x = (x - offsetx) / F437_CHAR_WIDTH;
  if((y < offsety) || (y > F437_CHAR_HEIGHT * fake437->height + offsety))
    *out_y = -1;
  else
    *out_y = (y - offsety) / F437_CHAR_HEIGHT;
}
